﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.Entitys;
using System.IO;

namespace Waker.Devices
{
    public partial class FormLEDIndoor : Base.FormBase
    {
        public FormLEDIndoor()
        {
            InitializeComponent();
        }
        private void FormLEDIndoor_Load(object sender, EventArgs e)
        {
            this.AutoScroll = true;

            this.icbLEDType.Properties.Items.AddRange(new DevExpress.XtraEditors.Controls.ImageComboBoxItem[] {
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("公司名称", 0, -1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("系统名称", 1, -1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("当前时间", 2, -1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("刷卡员工工地名称", 3, -1),            
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("刷卡员工分包商", 4, -1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("刷卡员工姓名", 5, -1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("刷卡员工工号", 6, -1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("刷卡时间", 7, -1),
            new DevExpress.XtraEditors.Controls.ImageComboBoxItem("自定内容", 8, -1)
            });

            txtContent.Enabled = false;


            this.spinEdit1.Value = 10;
            this.colorEdit1.Color = Color.Red;

            Screen screen;
            if (Screen.AllScreens.Length < 2)
                screen = Screen.AllScreens[0];
            else
                screen = Screen.AllScreens[1];
            this.PlContent.Width = screen.WorkingArea.Width / 2;
            this.PlContent.Height = screen.WorkingArea.Height / 2;
            if (PlContent.Width < this.Width)
                PlContent.Location = new Point((this.Width - PlContent.Width) / 2, PlContent.Location.Y);
            lblWidth.Text = screen.Bounds.Width.ToString();
            lblHeight.Text = screen.Bounds.Height.ToString();

            PictureBox PicEventPhoto = null;

            if (Session.ListInDoorLEDData != null && Session.ListInDoorLEDData.Count > 0)
            {
                foreach (var en in Session.ListInDoorLEDData)
                {
                    if (en.LEDType == Convert.ToInt32(Enums.Enum_LEDType.EventPhoto))
                    {
                        PicEventPhoto = new PictureBox();
                        PicEventPhoto.Location = new Point(en.LocationX, en.LocationY);
                        continue;
                    }
                    if (en.LEDTextHeight == 0 || en.LEDTextWidth == 0)
                        continue;
                    var lbl = new Label();
                    if (en.LEDType == Convert.ToInt32(Enums.Enum_LEDType.CustomText))
                    {
                        lbl.Text = en.LEDText;
                    }
                    else
                        lbl.Text = GetLEDTextByType((Enums.Enum_LEDType)en.LEDType);
                    lbl.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont.FontFamily, en.FontSize);
                    lbl.ForeColor = Color.FromName(en.ColorName);
                    lbl.Location = new Point(en.LocationX, en.LocationY);
                    lbl.Width = en.LEDTextWidth;
                    lbl.Height = en.LEDTextHeight;

                    lbl.BackColor = System.Drawing.Color.Transparent;
                    lbl.MouseMove += new System.Windows.Forms.MouseEventHandler(Common_MouseMove);
                    lbl.MouseDown += new System.Windows.Forms.MouseEventHandler(Common_MouseDown);
                    lbl.MouseUp += new System.Windows.Forms.MouseEventHandler(Common_MouseUp);
                    lbl.DoubleClick += new EventHandler(lbl_DoubleClick);
                    lbl.Tag = Convert.ToInt32(en.LEDType);

                    this.PlContent.Controls.Add(lbl);
                }
            }
            if (PicEventPhoto == null)
            {
                PicEventPhoto = new PictureBox();
                PicEventPhoto.Location = new Point(100, 100);
            }

            PicEventPhoto.Size = new Size(300, 200);
            PicEventPhoto.BorderStyle = BorderStyle.FixedSingle;
            PicEventPhoto.MouseMove += new System.Windows.Forms.MouseEventHandler(Common_MouseMove);
            PicEventPhoto.MouseDown += new System.Windows.Forms.MouseEventHandler(Common_MouseDown);
            PicEventPhoto.MouseUp += new System.Windows.Forms.MouseEventHandler(Common_MouseUp);
            this.PlContent.Controls.Add(PicEventPhoto);
        }



        private void icbLEDType_SelectedValueChanged(object sender, EventArgs e)
        {
            if (icbLEDType.EditValue != null
              && Convert.ToInt32(icbLEDType.EditValue) == Convert.ToInt32(Enums.Enum_LEDType.CustomText))
                txtContent.Enabled = true;
            else
                txtContent.Enabled = false;
            txtContent.Text = GetLEDTextByType((Enums.Enum_LEDType)icbLEDType.EditValue);
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (icbLEDType.EditValue == null)
            {
                UI.ShowError("请选择类型");
                return;
            }
            if (string.IsNullOrEmpty(txtContent.Text))
            {
                UI.ShowError("文本内容不能为空");
                return;
            }
            if (spinEdit1.Value < 10 || spinEdit1.Value > 100)
            {
                UI.ShowError("字体大小应该在10和100之间");
                return;
            }

            Label lbl = new Label();
            //Image image1 = Image.FromFile(@"D:\新建文件夹\Waker\Waker\bin\Debug\Images\02.gif");
            //lbl.Image = image1;
            //lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            lbl.MouseMove += new System.Windows.Forms.MouseEventHandler(Common_MouseMove);
            lbl.MouseDown += new System.Windows.Forms.MouseEventHandler(Common_MouseDown);
            lbl.MouseUp += new System.Windows.Forms.MouseEventHandler(Common_MouseUp);
            lbl.DoubleClick += new EventHandler(lbl_DoubleClick);
            lbl.Tag = icbLEDType.EditValue;


            lbl.Text = txtContent.Text;
            lbl.Location = new Point(0, 0);
            lbl.AutoSize = true;
            lbl.BackColor = System.Drawing.Color.Transparent;
            lbl.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont.FontFamily, (float)spinEdit1.Value);
            lbl.ForeColor = colorEdit1.Color;

            this.PlContent.Controls.Add(lbl);

            spinEdit1.Enabled = icbLEDType.Enabled = true;
            CannelLabelSelect();
        }
        void lbl_DoubleClick(object sender, EventArgs e)
        {
            var lbl = sender as Label;
            if (lbl == null)
                return;
            if (lbl.Tag == null)
                return;
            CannelLabelSelect();
            if (btnOK.Tag != null)
            {
                btnOK.Tag = btnDel.Tag = null;
                spinEdit1.Enabled = icbLEDType.Enabled = true;
                return;
            }
            else
            {
                lbl.BorderStyle = BorderStyle.FixedSingle;
                spinEdit1.Enabled = false;
                icbLEDType.Enabled = false;
                icbLEDType.EditValue = lbl.Tag;
                colorEdit1.Color = lbl.ForeColor;
                spinEdit1.Value = Convert.ToDecimal(lbl.Font.Size);
                txtContent.Text = lbl.Text;
                btnOK.Tag = lbl;
                btnDel.Tag = lbl;
            }
        }
        private void btnDel_Click(object sender, EventArgs e)
        {
            var lbl = btnDel.Tag as Label;
            if (lbl == null)
                return;
            if (!UI.ShowOKCancelReturnBool(string.Format("确定要移除  {0}", lbl.Text)))
                return;
            this.PlContent.Controls.Remove(lbl);
            spinEdit1.Enabled = icbLEDType.Enabled = true;
            btnDel.Tag = null;
            CannelLabelSelect();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var lbl = btnOK.Tag as Label;
            if (lbl == null)
                return;
            if (spinEdit1.Value < 10 || spinEdit1.Value > 100)
            {
                UI.ShowError("字体大小应该在10和100之间");
                return;
            }
            lbl.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont.FontFamily, (float)spinEdit1.Value);
            lbl.ForeColor = colorEdit1.Color;
            lbl.Text = txtContent.Text;
            spinEdit1.Enabled = icbLEDType.Enabled = true;
            btnOK.Tag = null;
            CannelLabelSelect();
        }

        private void CannelLabelSelect()
        {
            foreach (var control in this.PlContent.Controls)
            {
                var l = control as Label;
                if (l != null)
                    l.BorderStyle = BorderStyle.None;
            }
        }
        private void barBtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.PlContent.Controls.Count < 1)
            {
                UI.ShowError("屏幕的内容为空,请添加后保存.");
                return;
            }
            List<En_LEDData> list = new List<En_LEDData>();
            foreach (Control control in this.PlContent.Controls)
            {
                var en = new En_LEDData();
                if (control is PictureBox)
                {
                    var pic = (PictureBox)control;
                    en.LEDType = Convert.ToInt32(Enums.Enum_LEDType.EventPhoto);
                    en.LocationX = pic.Location.X;
                    en.LocationY = pic.Location.Y;
                    list.Add(en);
                    continue;
                }
                if (control is Label)
                {
                    var lbl = (Label)control;
                    if (lbl.PreferredSize.Height == 0 || lbl.PreferredSize.Width == 0)
                        continue;
                    en.LEDType = Convert.ToInt32(lbl.Tag);
                    if (en.LEDType == Convert.ToInt32(Enums.Enum_LEDType.CustomText))
                        en.LEDText = lbl.Text;
                    en.LEDTextWidth = lbl.PreferredSize.Width;
                    en.LEDTextHeight = lbl.PreferredSize.Height;
                    en.FontSize = Convert.ToInt32(lbl.Font.Size);
                    en.ColorName = lbl.ForeColor.Name;
                    en.LocationX = lbl.Location.X;
                    en.LocationY = lbl.Location.Y;
                    list.Add(en);
                }
            }
            var s = XmlHelper<En_LEDData>.EntityToXml(list);
            var xd = new System.Xml.XmlDocument();
            xd.LoadXml(s);
            xd.Save(Consts.LEDInDoorXmlPath);
            Session.ListInDoorLEDData = list;
            UI.ShowInfo("保存成功");
        }
        #region 通用事件
        private Point mouse_offset;
        private Point original_pos;
        private void Common_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                //检查是否超出背景图片边界，超出则位置不变；在图片范围内，则控件位置改变。
                //if (((Control)sender).Parent.PointToClient(mousePos).X >= 0 && (((Control)sender).Parent.PointToClient(mousePos).X <= ((Control)sender).Parent.BackgroundImage.Size.Width)
                //    && (((Control)sender).Parent.PointToClient(mousePos).Y >= 0 && ((Control)sender).Parent.PointToClient(mousePos).Y <= ((Control)sender).Parent.BackgroundImage.Size.Height))
                if (((Control)sender).Parent.PointToClient(mousePos).X >= 0 && (((Control)sender).Parent.PointToClient(mousePos).X <= ((Control)sender).Parent.Size.Width)
                                   && (((Control)sender).Parent.PointToClient(mousePos).Y >= 0 && ((Control)sender).Parent.PointToClient(mousePos).Y <= ((Control)sender).Parent.Size.Height))
                {
                    ((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
                    //MessageBox.Show(((Control)sender).Location.ToString());
                }
                else
                {
                    ((Control)sender).Location = original_pos;
                }
            }
        }
        private void Common_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);
            original_pos = ((Control)sender).Location;
        }

        private void Common_MouseMove(object sender, MouseEventArgs e)
        {
            ((Control)sender).Cursor = Cursors.Arrow;
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                ((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
            }
        }
        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            Common_MouseUp(sender, e);
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            Common_MouseDown(sender, e);
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            Common_MouseMove(sender, e);
        }
        #endregion

        private string GetLEDTextByType(Enums.Enum_LEDType type)
        {
            switch (type)
            {
                case Enums.Enum_LEDType.CommanyName:
                    return Session._CommanyName;
                case Enums.Enum_LEDType.SystemName:
                    return Consts.System_Name;
                case Enums.Enum_LEDType.CurrentTime:
                    return "当前时间" + DateTime.Now.ToString(Consts.StrDateTimeFormat);
                case Enums.Enum_LEDType.ConstructionName:
                    return "刷卡员工工地名称";

                case Enums.Enum_LEDType.SubContractorName:
                    return "刷卡员工分包商";
                case Enums.Enum_LEDType.EmployeeName:
                    return "刷卡员工姓名";
                case Enums.Enum_LEDType.EmployeeNo:
                    return "刷卡员工工号";
                case Enums.Enum_LEDType.EventTime:
                    return "刷卡时间" + DateTime.Now.ToString(Consts.StrDateTimeFormat);
                case Enums.Enum_LEDType.CustomText:
                default:
                    return txtContent.Text;
            }
        }

        private void barBtnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
    }
}
